//
//  ZZChangePasswordViewController.m
//  新项目
//
//  Created by liGordon on 17/3/28.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZChangePasswordViewController.h"
#import "ZZPassWordViewModel.h"
@interface ZZChangePasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *oldPWInput;
@property (weak, nonatomic) IBOutlet UITextField *NewPWInput;
@property (weak, nonatomic) IBOutlet UITextField *RNewPWInput;
@property (strong, nonatomic) UIAlertView *netAlert;
@end

@implementation ZZChangePasswordViewController
@synthesize netAlert = _netAlert;
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(clickForSend)];
    self.navigationItem.rightBarButtonItem=item;}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)clickForSend
{
    if (self.oldPWInput.text.length <= 0) {
        [MBProgressHUD showError:@"请输入原密码"];
    }else if (self.NewPWInput.text.length <= 0){
        [MBProgressHUD showError:@"请输入新密码"];
    }else if (self.RNewPWInput.text.length <=0){
        [MBProgressHUD showError:@"请再次输入新密码"];
    }else if (![self.NewPWInput.text isEqualToString:self.RNewPWInput.text])
     [MBProgressHUD showError:@"两次输入密码不同请重新输入"];
    else{
        NSLog(@"掉接口");
        NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
        NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
        [[ZZPassWordViewModel shareViewModel]changePassWordWithLoginName:loginDict[@"LoginName"]andNewPassWord:self.NewPWInput.text andOldPassWord:self.oldPWInput.text andSuccess:^(NSDictionary *resultObj) {
            if(resultObj){
                NSInteger theCode = [[resultObj objectForKey:@"code"] integerValue];
                if(theCode){
                    NSDictionary *sucDict = [WHJSON objectFormJsonString:[resultObj objectForKey:@"resultJson"]];
                    sucDict = [DMToolsObject replaceNullObjctWithDict:sucDict];
                    [MBProgressHUD showSuccess:@"修改成功"];
                    
                }else{
                    NSString *messageStr = [resultObj objectForKey:@"message"];
                    if(messageStr == nil || [messageStr isEqualToString:@""]){
                        messageStr = @"修改失败！";
                    }
                    [self showAlertActionWith:messageStr];
                }
            }else{
                NSString *messageStr = @"修改失败！";
                [self showAlertActionWith:messageStr];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                           [MBProgressHUD hideHUD];
            });
    
            
        } andFailer:^{
            [MBProgressHUD hideHUD];
            NSString *messageStr = @"请求失败！";
            [self showAlertActionWith:messageStr];
        }];
    }
   
}


- (void)showAlertActionWith:(NSString *)theAlertMsg {
    if(!_netAlert){
        _netAlert = [[UIAlertView alloc] initWithTitle:nil message:theAlertMsg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    }
    _netAlert.message = theAlertMsg;
    [_netAlert show];
}

@end
